package com.hk.employeemanagementsystem.web.controller;

import com.hk.employeemanagementsystem.entity.JobLevelEntity;
import com.hk.employeemanagementsystem.entity.PositionEntity;
import com.hk.employeemanagementsystem.service.JobLevelEntityService;
import com.hk.employeemanagementsystem.util.web.WebCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author by 何坤
 * @Classname JobLevelController
 * @Description TODO
 * @Date 2020/1/7 22:26
 */
@RestController
@RequestMapping(value = "/jobLevel")
public class JobLevelController {
    private Logger logger = LoggerFactory.getLogger(JobLevelController.class);

    @Autowired
    private JobLevelEntityService jobLevelEntityServiceImpl;

    @GetMapping(value = "/get")
    public Map<String, Object> selectAllJobLevel(){
        Map<String, Object> tips = new HashMap<>();
        try {
            List<JobLevelEntity> jobLevelEntities = jobLevelEntityServiceImpl.selectAllJobLevel();
            tips.put(WebCode.SUCCESS, true);
            tips.put(WebCode.MSG, WebCode.SUCCESS_SELECT);
            tips.put("data", jobLevelEntities);
        } catch (Exception e) {
            logger.info(e.getMessage());
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_SELECT);
        }
        return tips;
    }

    @PutMapping(value = "/update")
    public Map<String, Object> updateJobLevel(@RequestBody JobLevelEntity jobLevelEntity){
        Map<String, Object> tips = new HashMap<>();
        try {
            jobLevelEntityServiceImpl.updateJobLevel(jobLevelEntity);
            tips.put(WebCode.SUCCESS, true);
            tips.put(WebCode.MSG, WebCode.SUCCESS_UPDATE);
        } catch (Exception e) {
            logger.info(e.getMessage());
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_UPDATE);
        }
        return tips;
    }

    @PostMapping(value = "/save")
    public Map<String, Object> saveJobLevel(@RequestBody JobLevelEntity jobLevelEntity){
        Map<String, Object> tips = new HashMap<>();
        try {
            jobLevelEntityServiceImpl.saveJobLevel(jobLevelEntity);
            tips.put(WebCode.SUCCESS, true);
            tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
        } catch (Exception e) {
            logger.info(e.getMessage());
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_SAVE);
        }
        return tips;
    }

    @DeleteMapping(value = "/delete/{ids}")
    public Map<String, Object> deleteJobLevel(@PathVariable String ids){
        Map<String, Object> tips = new HashMap<>();
        try {
            String[] split = ids.split(",");
            jobLevelEntityServiceImpl.deleteJobLevel(Arrays.asList(split));
            tips.put(WebCode.SUCCESS, true);
            tips.put(WebCode.MSG, WebCode.SUCCESS_DELETE);
        } catch (Exception e) {
            logger.info(e.getMessage());
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_DELETE);
        }
        return tips;
    }

    @GetMapping(value = "/validate")
    public Map<String, Object> validatePositionName(@RequestParam String name,
                                                    @RequestParam(required = false) Integer id){
        Map<String, Object> tips = new HashMap<>();
        try {
            Boolean exist = jobLevelEntityServiceImpl.validateJobLevelName(name, id);
            tips.put(WebCode.SUCCESS, true);
            tips.put(WebCode.MSG, WebCode.SUCCESS_DELETE);
            tips.put("data", exist);
        } catch (Exception e) {
            logger.info(e.getMessage());
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_DELETE);
        }
        return tips;
    }
}
